package com.bltec.emergency.controller.foreignController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ForeignVesselNumber;
import com.bltec.emergency.service.foreignService.ForeignVesselNumberService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/foreignVesselNumber")
@RequiresRoles("superManger")
public class ForeignVesselNumberController {
    @Resource
    private ForeignVesselNumberService foreignVesselNumberService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllForeignVesselNumber")
    public ResponseJson getUser() {
        try {
            List<ForeignVesselNumber> ForeignVesselNumber = foreignVesselNumberService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(ForeignVesselNumber));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*     private String shipName;
    private BigDecimal dailyFee;
    private BigDecimal fuelPrice;
    private String rescueArea;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rescueBeginTime;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date rescueEndTime;
    private BigDecimal rescueFuel;
    private Long rescueNum;*/
    @RequestMapping("addForeignVesselNumber")
    public ResponseJson addUser(String shipName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime, BigDecimal rescueFuel, Long rescueNum) {
        try {
            ForeignVesselNumber foreignVesselNumber = new ForeignVesselNumber();
            foreignVesselNumber.setShipName(shipName);
            foreignVesselNumber.setDailyFee(dailyFee);
            foreignVesselNumber.setFuelPrice(fuelPrice);
            foreignVesselNumber.setRescueArea(rescueArea);
            foreignVesselNumber.setRescueBeginTime(rescueBeginTime);
            foreignVesselNumber.setRescueEndTime(rescueEndTime);
            foreignVesselNumber.setRescueFuel(rescueFuel);
            foreignVesselNumber.setRescueNum(rescueNum);
            foreignVesselNumber.setEnable(true);
            foreignVesselNumber.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignVesselNumber.setCreateTime(DateUtil.dateNow());
            foreignVesselNumberService.add(foreignVesselNumber);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }


    @RequestMapping("updateForeignVesselNumber")
    public ResponseJson updateUser(Long id,String shipName, BigDecimal dailyFee, BigDecimal fuelPrice, String rescueArea, Date rescueBeginTime, Date rescueEndTime,BigDecimal rescueFuel,Long rescueNum) {
        try {
            ForeignVesselNumber foreignVesselNumber = foreignVesselNumberService.getForeignVesselNumberById(id);
            foreignVesselNumber.setShipName(shipName);
            foreignVesselNumber.setDailyFee(dailyFee);
            foreignVesselNumber.setFuelPrice(fuelPrice);
            foreignVesselNumber.setRescueArea(rescueArea);
            foreignVesselNumber.setRescueBeginTime(rescueBeginTime);
            foreignVesselNumber.setRescueEndTime(rescueEndTime);
            foreignVesselNumber.setRescueFuel(rescueFuel);
            foreignVesselNumber.setRescueNum(rescueNum);
            foreignVesselNumber.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignVesselNumber.setUpdateTime(DateUtil.dateNow());
            foreignVesselNumberService.updateForeignVesselNumberById(foreignVesselNumber);
            return new ResponseJson(Constant.OK, JSON.toJSON(foreignVesselNumber));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteForeignVesselNumber")
    public ResponseJson setDisable(Long id) {
        if (null != foreignVesselNumberService.getForeignVesselNumberById(id)) {
            if (foreignVesselNumberService.deleteForeignVesselNumberById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}